package cn.liyunqi.prplug.util;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

/**
 * @Auther: li.yunqi
 * @Date: 2019/10/31 16:08
 * @Description: TODO
 */
public class ImageUtil {

    public static File getScreenshot(String fileName,String formatName,int x,int y,int width,int height){

        try {
            BufferedImage screenshot = (new Robot()).createScreenCapture(new Rectangle(x, y, width, height));
            // 根据文件前缀变量和文件格式变量，自动生成文件名
            //String name="." + File.separator + APPDATA_FOLDER + File.separator  + "GuiCamera_" +"test3.png";
            File file = new File(fileName);
            // 将screenshot对象写入图像文件
            ImageIO.write(screenshot,formatName, file);
            return file;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    //光标的蓝色
    public static final int BLUE = -13792021;
    public static final int BLACK = -13553359;

    public static Integer getBlueIndexInImage(File file) throws IOException {
        //读取图片文件
        BufferedImage bi = null;
        try {
            bi = ImageIO.read(file);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //得到图片像su的宽和高 图片将会取中线
        int width = bi.getWidth();
        int height = bi.getHeight();
        //输出每一个像素点的color
        int j = height/2;
        for (int i = 0; i < width; i++) {

            int pixel = bi.getRGB(i, j);
            if(pixel == BLUE){
                return i;
            }

        }
        return null;
    }

    public static void main(String[] args) throws IOException {
        getBlueIndexInImage(new File("./pr-plug-data/vedio-track.png"));
    }

}